package com.jie.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.commonutils.PageUtils;
import com.jie.eduservice.entity.EduTeacher;
import com.jie.eduservice.entity.vo.TeacherQueryVO;
import com.jie.eduservice.mapper.EduTeacherMapper;
import com.jie.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author xzj
 * @since 2020-10-01
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    private final EduTeacherMapper teacherMapper;

    @Autowired
    public EduTeacherServiceImpl(EduTeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }

    @Override
    public Page<EduTeacher> pageTeacherCondition(Long current, Long limit, TeacherQueryVO teacherQueryVO) {
        // 创建Page对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        // 构建查询条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        // 判断封装查询对象是否为空
        if (teacherQueryVO == null) {
            teacherMapper.selectPage(teacherPage, null);  // 无条件
            return teacherPage;
        }

        // 多条件组合查询
        // 判断条件值是否为空, 不为空则拼接条件(动态sql)
        String name = teacherQueryVO.getName();
        Integer level = teacherQueryVO.getLevel();
        String begin = teacherQueryVO.getBegin();
        String end = teacherQueryVO.getEnd();
        if (!StringUtils.isEmpty(name)) {
            // 构建条件
            wrapper.like("name", name);  // 本来想用wrapper的isNotNull的, 但是因为没有判断空字符串, 就算了
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        // 排序查询DESC(根据创建时间)
        wrapper.orderByDesc("gmt_create");

        // 调用方法实现条件查询分页
        teacherMapper.selectPage(teacherPage, wrapper);
        return teacherPage;  // 返回分页的结果
    }

    @Override
    public Map<String, Object> getTeacherFrontList(Long current, Long limit) {
        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        // 将分页数据封装到page中
        baseMapper.selectPage(page, wrapper);

        // 将分页数据获取出来, 放到map集合中
        // 返回所有数据
        return PageUtils.pageToMap(page);
    }

    // 已经在父类中注入了Mapper了...服了, 真是牛逼

}
